package com.bawei.day15_service2;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;
import android.widget.Toast;

import androidx.annotation.Nullable;
//
public class MusicService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBinder();
    }
    class MusicBinder extends Binder{
        public void play(){
            Toast.makeText(MusicService.this, "服务：播放音乐", Toast.LENGTH_SHORT).show();
        }
        public void pre(){
            Toast.makeText(MusicService.this, "服务：上一首", Toast.LENGTH_SHORT).show();
        }
        public void next(){
            Toast.makeText(MusicService.this, "服务：下一首", Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Notification.Builder builder = new Notification.Builder(this);//服务本身就是上下文
        builder.setSmallIcon(R.drawable.ic_launcher_background);
//        builder.setContentTitle("我是标题");
//        builder.setContentText("我是内容");
        //设置自定义布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
        //点击
        Intent intent1 = new Intent();
        intent1.setAction("com.bawei.pre");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 101, intent1, PendingIntent.FLAG_ONE_SHOT);
        remoteViews.setOnClickPendingIntent(R.id.iv_pre,pendingIntent);

        Intent intent2 = new Intent();
        intent2.setAction("com.bawei.play");
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 101, intent2, PendingIntent.FLAG_ONE_SHOT);
        remoteViews.setOnClickPendingIntent(R.id.iv_play,pendingIntent2);

        Intent intent3 = new Intent();
        intent3.setAction("com.bawei.next");
        PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 101, intent3, PendingIntent.FLAG_ONE_SHOT);
        remoteViews.setOnClickPendingIntent(R.id.iv_next,pendingIntent3);

        builder.setCustomContentView(remoteViews);//设置自定义布局


        Notification notification = builder.build();
        startForeground(1,notification);//开启前台服务
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);//true代表停止前台服务的时候，通知消失
    }
}
